import json

from django.shortcuts import render,redirect
from django.urls import reverse
from system.models import User,Goods,Unit,GoodsType,TransactionRecords,Log

import socket    #获取ip地址用

#接口
from django.http import HttpResponse,JsonResponse
from django.views.decorators.csrf import csrf_exempt

from system.views import SUCCESS_CODE,ERR_CODE,ERR_TIMEOUT,SUCCESS_MESSAGE,ERR_MESSAGE


# 添加商品单位
@csrf_exempt
def addGoodsUnit(request):
    dic = {}
    if request.method == "POST":
        name = request.POST.get('name')
        if name:
            goods = Unit.objects.filter(name=name)
            if goods:
                dic['code'] = ERR_CODE
                dic['message'] = '该商品单位已存在'
            else:
                goods_type = Unit()
                goods_type.name = name
                goods_type.save()

                dic['code'] = SUCCESS_CODE
                dic['message'] = '添加成功'
        else:
            dic['code'] = ERR_CODE
            dic['message'] = '数值不能为空'
    else:
        dic['code'] = ERR_CODE
        dic['message'] = ERR_MESSAGE
    return JsonResponse(dic, json_dumps_params={'ensure_ascii': False})

# 删除商品单位类别
@csrf_exempt
def deleteGoodsUnit(request):
    dic = {}
    if request.method == "POST":
        id = request.POST.get('id')
        type = Unit.objects.filter(id=id)
        if type:
            type[0].delete()
            dic['code'] = SUCCESS_CODE
            dic['message'] = SUCCESS_MESSAGE
        else:
            dic['code'] = ERR_CODE
            dic['message'] = '商品单位不存在'
    else:
        dic['code'] = ERR_CODE
        dic['message'] = ERR_MESSAGE
    return JsonResponse(dic, json_dumps_params={'ensure_ascii': False})

# 获取商品单位列表
@csrf_exempt
def GoodsUnitInfo(request):
    dic = {}
    if request.method == "POST":
        info = Unit.objects.all()
        if info:
            unitList = []
            for i in info:
                data = {}
                data['id'] = i.id
                data['name'] = i.name
                unitList.append(data)
            dic['code'] = SUCCESS_CODE
            dic['message'] = SUCCESS_MESSAGE
            dic['data'] = {
                'unitList': unitList
            }
        else:
            dic['code'] = SUCCESS_CODE
            dic['message'] = '暂时没有商品单位'
    else:
        dic['code'] = ERR_CODE
        dic['message'] = ERR_MESSAGE
    return JsonResponse(dic, json_dumps_params={'ensure_ascii': False})

"""获取商品单位数据"""
@csrf_exempt
def getUnitData(request):
    dic = {}
    if request.method == 'POST':
        info = Unit.objects.all()
        if info:
            unitList = []
            for i in info:
                data = {}
                data['id'] = i.id
                data['name'] = i.name
                unitList.append(data)

            dic['code'] = SUCCESS_CODE
            dic['message'] = SUCCESS_MESSAGE

            pageSize = int(request.POST.get('pageSize'))
            page = int(request.POST.get('pageId'))
            minSize = (page - 1) * pageSize
            maxSize = page * pageSize

            if maxSize >= len(unitList):
                dic['data'] = {
                    'unitList': unitList[minSize:],
                    'size': len(unitList)
                }
            else:
                dic['data'] = {
                    'unitList': unitList[minSize: maxSize],
                    'size': len(unitList)
                }
        else:
            dic['code'] = SUCCESS_CODE
            dic['message'] = '暂时没有商品单位'
    else:
        dic['code'] = ERR_CODE
        dic['message'] = ERR_MESSAGE
    return JsonResponse(dic, json_dumps_params={'ensure_ascii': False})

# 修改商品单位
@csrf_exempt
def editGoodsUnit(request):
    dic = {}
    if request.method == 'POST':
        type_id = request.POST.get('id')
        name = request.POST.get('name')

        unit = Unit.objects.filter(id=type_id)[0]
        if unit:
            unit.name = name
            unit.save()

            dic['code'] = SUCCESS_CODE
            dic['message'] = SUCCESS_MESSAGE
        else:
            dic['code'] = ERR_CODE
            dic['message'] = '找不到该商品单位'
    else:
        dic['code'] = ERR_CODE
        dic['message'] = ERR_MESSAGE
    return JsonResponse(dic, json_dumps_params={'ensure_ascii': False})